package com.hhq.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhq.system.common.model.Option;
import com.hhq.system.common.result.PageResult;
import com.hhq.system.common.result.Result;
import com.hhq.system.model.form.SwitchTenantForm;
import com.hhq.system.model.form.TenantForm;
import com.hhq.system.model.query.TenantPageQuery;
import com.hhq.system.model.vo.TenantPageVO;
import com.hhq.system.model.vo.TenantVO;
import com.hhq.system.plugin.norepeat.annotation.PreventRepeatSubmit;
import com.hhq.system.service.SysTenantService;
import com.hhq.system.service.SysUserTenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 租户控制层
 *
 * @author Ray
 * @since 2022/10/16
 */
@Tag(name = "03.租户接口")
@RestController
@RequestMapping("/api/v1/tenant")
@RequiredArgsConstructor
public class SysTenantController {

    private final SysTenantService sysTenantService;

    private final SysUserTenantService sysUserTenantService;

    @Operation(summary = "租户分页列表")
    @GetMapping("/page")
    public PageResult<TenantPageVO> getRolePage(TenantPageQuery queryParams) {
        Page<TenantPageVO> result = sysTenantService.getTenantPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "租户下拉列表")
    @GetMapping("/options")
    public Result<List<Option>> listTenantOptions() {
        List<Option> list = sysTenantService.listTenantOptions();
        return Result.success(list);
    }

    @Operation(summary = "新增租户")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:tenant:add')")
    @PreventRepeatSubmit
    public Result<Long> addTenant(@Valid @RequestBody TenantForm tenantForm) {
        return Result.success(sysTenantService.saveTenant(tenantForm));
    }

    @Operation(summary = "租户表单数据")
    @GetMapping("/{tenantId}/form")
    public Result<TenantForm> getRoleForm(@Parameter(description = "租户ID") @PathVariable Long tenantId) {
        return Result.success(sysTenantService.getTenantForm(tenantId));
    }

    @Operation(summary = "修改租户")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('sys:tenant:edit')")
    public Result<Long> updateRole(@Valid @RequestBody TenantForm tenantForm) {
        return Result.success(sysTenantService.saveTenant(tenantForm));
    }

    @Operation(summary = "删除租户")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:tenant:delete')")
    public Result<Long> deleteRoles(@Parameter(description = "删除租户，多个以英文逗号(,)拼接") @PathVariable String ids) {
        boolean result = sysTenantService.deleteByIds(ids);
        return Result.judge(result);
    }

    @Operation(summary = "切换租户")
    @PostMapping("/switchTenant")
    public Result<Boolean> switchTenant(@Valid @RequestBody SwitchTenantForm switchTenantForm) {
        return Result.judge(sysTenantService.switchTenant(switchTenantForm.getId()));
    }

    @Operation(summary = "获取用户租户列表")
    @GetMapping("/userTenants")
    public Result<List<TenantVO>> userTenants() {
        List<TenantVO> tenants = sysUserTenantService.getTenants();
        return Result.success(tenants);
    }

}
